﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pharmacy.Entities;
using Pharmacy.Data;
using System.Collections.Generic;

public partial class Admin_StockWarning : System.Web.UI.Page
{
    private TList<Stock> all;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            tb_day.Text = "365";
           // tb_ToDate.Text = DateTime.Now.AddYears(1).ToString("dd-MMM-yyyy");
            btn_Search_Click(sender, null);
        }
    }


    protected void btn_Search_Click(object sender, ImageClickEventArgs e)
    {
        //  List<> listStock = new List<Stock>();
        //List<Stock> listStock = new List<Stock>();

        VList<StockWarning> listExp = new VList<StockWarning>();
        VList<StockWarning> listsmallMinStock = new VList<StockWarning>();
        VList<StockWarning> listlarMinStock = new VList<StockWarning>();
        int AllItem=0,NearestExp = 0, valid = 0, Invalid = 0, larMinStock = 0, smallMinStock = 0, AlreadyReminder = 0;
       // DateTime fromDate = Convert.ToDateTime(tb_FromDate.Text);
        //DateTime toDate = Convert.ToDateTime(tb_ToDate.Text);
        int numdayExp = 0;
        numdayExp = Convert.ToInt32(tb_day.Text);
        Stock objStock;
        foreach (Pharmacy.Entities.StockWarning stockwarning in DataRepository.StockWarningProvider.GetAll())
        {
            AllItem += 1;
            if (stockwarning.InStock > stockwarning.MinStock && stockwarning.InStock < stockwarning.MaxStock)
            {
                larMinStock += 1;
                // objStock = DataRepository.StockProvider.GetMax();
                listlarMinStock.Add(stockwarning);
            }
            else if (stockwarning.InStock < stockwarning.MinStock)
            {
                smallMinStock += 1;
                listsmallMinStock.Add(stockwarning);
                
            }
            else if (stockwarning.NearestExp <= numdayExp)
            {
                NearestExp += 1;
                listExp.Add(stockwarning);
            }
        }
        valid = AllItem - larMinStock - smallMinStock - NearestExp;
        Invalid = AllItem - valid;

        //foreach (Pharmacy.Entities.Stock stock in DataRepository.StockProvider.GetMax("All"))
        //{
        //    objDrugDispo = DataRepository.DrugDispoProvider.GetByDrugId(stock.DrugId);
        //    //if (stock.quantity>objdrugdispo.maxstock)
        //    //     response.write(@"<script language='javascript'>alert('" + stock.drugid + " " +  stock.quantity +" ')</script>");

        //    //if (cardholder.InvalidCard == true)
        //    //    InvalidCard += 1;
        //    //else if (cardholder.Exception == true)
        //    //    Exception += 1;
        //    //else
        //    //    if (cardholder.ExpDate > toDate)
        //    //        ValidCard += 1;
        //    //    else
        //    //        if (cardholder.ExpAlert == "SentToRenewal")
        //    //            AlreadyReminder += 1;
        //    //        else
        //    if (stock.Quantity > objDrugDispo.MaxStock)
        //    {
        //        larMaxStock += 1;
        //        listNeedToReminder.Add(stock);
        //    }
        //    if (stock.Quantity < objDrugDispo.MinStock)
        //    {
        //       smallMinStock += 1;
        //        listCannotReminder.Add(stock);
        //    }
        //}
        //div_ValidStock.InnerHtml = "<b>" + AllItem.ToString() + "</b> All Stock <br/>";
        div_ValidStock.InnerHtml = "<b>" + valid.ToString() + "</b> valid Stock <br/>";
        div_ValidStock.InnerHtml += "<b>" + Invalid.ToString() + "</b> Invalid Stock";
        div_AlreadyReminder.InnerHtml = "<b>" + NearestExp.ToString() + "</b> NearestExp <br/>";
        div_larMinStock.InnerHtml = "<b>" + larMinStock.ToString() + "</b> larMinStock <br/>";
        div_smallMinStock.InnerHtml = "<b>" + smallMinStock.ToString() + "</b> smallMinStock <br/>";
        //  div_CannotReminder.InnerHtml += "<b>" + CannotReminder.ToString() + "</b> wrong email address <br/>";
        listExp.Sort("NearestExp"); //sort by NearestExp
        listsmallMinStock.Sort("MinStock"); //sort by Minstock
        listlarMinStock.Sort("MinStock");// sort by min stock
        grv_AlreadyReminder.DataSource = listExp;
        grv_AlreadyReminder.DataBind();
        grv_larMinStock.DataSource = listlarMinStock;
        grv_larMinStock.DataBind();
        grv_smallMinStock.DataSource = listsmallMinStock;
        grv_smallMinStock.DataBind();
    }
}
